/*
大赛排名
CompetitionRankings.js*/

import React, {Component,PropTypes} from 'react';
import {connect} from 'dva';
import {routerRedux,withRouter } from 'dva/router';
import { Row,Col,Button,Card,Tabs,Popconfirm } from 'antd';
import {
	DivisionSelect,
	DateRangeChoice,
	BranchOfficeChoice,
	BDepartmentChoice } from '../../components/SelectToolBar';
import RankingTable from './RankingsTable';
import moment from 'moment';
class CompetitionRankings extends Component{
	state={
		"author":'',
		"BranchOffice":[],
		"BusinessDepartment":[],
	}
	componentWillMount() {
		const { login } = this.props;
		const { departmentData,EnrollData } = this.props.privateGame;
		const { author } = this.state;
		const BOffice = [];
		console.log(112345,this.props.privateGame)
		const authorities = login.userInfo.authorities;
		if(!_.isEmpty(login.userInfo)){
			login.userInfo.authorities.map((i)=>{
				if(i == 'ROLE_HEADQUARTERSDOCKING' || i=='ROLE_ADMIN'){
					this.setState({
						"author":'0',
					})
				}else if(i == 'ROLE_BRANCHDOCKING'){
					this.setState({
						"author":'2',
					})
				}else{
					this.setState({
						"author":'1',
					})
				}
			})
		}
		departmentData.map((item,i)=>{
			if(item.parentId == 'ZZ001041'){
				BOffice.push(item)
			}
		})
		this.setState({
			'BranchOffice':BOffice
		})
	}
	componentDidMount(){
    this.fetchData(this.props)
  }
  componentWillReceiveProps(nextProps) {
    console.log('nextProps>cwr',nextProps,this.props)
    const { BOfficeCode,departmentData } = nextProps.privateGame;
    const {author}=this.state;
    const BDepartment = [];
    if(BOfficeCode !==''){
    	departmentData.map((item)=>{
    		if(BOfficeCode == item.parentId){
    			BDepartment.push(item)
    		}
    	})
    	this.setState({
    		'BusinessDepartment':BDepartment
    	})
    }
    if(author=='2'&&!_.isEmpty(departmentData)){
	  	departmentData.map((item)=>{
	  		if(item.parentId !== "ZZ001041"){
	  			BDepartment.push(item)
	  		}
	  	})
	  	this.setState({
    		'BusinessDepartment':BDepartment
    	})
	  }
  }
  fetchData = (props) => {
    console.log('fetchData>fetchData',props)
    this.props.dispatch({
      type: `privateGame/getRankingsTable`,
      payload:props
    })
  }
  ranksSearch=()=>{
  	// console.log('rankssearch',privateGame)
  	console.log(4564646,this.props)
  	this.props.dispatch({
      type: `privateGame/getRankingsTable`,
      payload:this.props
    })
  }
	render(){
		const {
			DetailsData,
			beginDate,
			endDate,
			RbeginDate,
			RendDate,
			lastDate,
			firstDate,
			BOfficeCode,
			BDepartmentName,
			departmentData
		}=this.props.privateGame;
		const {author,BranchOffice,BusinessDepartment} = this.state;
		moment(RbeginDate).format("YYYYMMDD")
		moment(RendDate).format("YYYYMMDD")
		console.log('ccccccc',this.props.privateGame,11234,this.state)
		return (
			<div>
				<Row type="flex" justify="start" style={{marginBottom:20}}>
					<Col span={6} style={{marginTop:8}}>
						<label>赛区:&nbsp;</label>
						<DivisionSelect
							dispatch={this.props.dispatch}
							data={DetailsData.divisionList}
							tag='privateGame'
						/>
					</Col>
					<Col span={8} style={{marginTop:8}}>
						<label>选择日期:&nbsp;</label>
						<DateRangeChoice
							dispatch={this.props.dispatch}
							tag='privateGame'
							beginDate={RbeginDate}
							endDate={RendDate}
							firstDate={firstDate}
							lastDate={lastDate}
						/>
					</Col>
					{
						author=='0'?<Col span={6} style={{marginTop:8}}>
							<label>分公司:&nbsp;</label>
							<BranchOfficeChoice
								value={BOfficeCode&&BOfficeCode!==''?departmentData.find(item=>item.code==BOfficeCode).name:'请选择分公司'}
								dispatch={this.props.dispatch}
								tag='privateGame'
								data={BranchOffice}
								allData={departmentData}
							/>
						</Col>:null
					}
					
					{
						BOfficeCode||BusinessDepartment!==[]&&author=='2'?<Col span={8} style={{marginTop:8}}>
							<label>营业部:&nbsp;</label>
							<BDepartmentChoice
								value={BDepartmentName&&BDepartmentName!==''?departmentData.find(item=>item.id==BDepartmentName).name:'请选择营业部'}
								dispatch={this.props.dispatch}
								tag='privateGame'
								data={BusinessDepartment}
							/>
						</Col>:null
					}
					<Col span={2} style={{marginTop:8}}>
						{
			  			moment(RbeginDate).isBefore(RendDate)||RbeginDate==''?<Button type="primary" onClick={this.ranksSearch}>查询</Button>:<Button type="primary" disabled>查询</Button>
						}
					</Col>
					
				</Row>
				<RankingTable/>
			</div>
		)
	}
}


export default connect(({privateGame,login})=>({privateGame,login}))(CompetitionRankings)